<?php
##################################
# adsm
##################################

# variables
$page['L_message'] = "";
$page['form_action'] = convert_url("index.php?r=" . $plugin_idurl .
    "&v1=form_adsm");
$nb_erreur = "0";
$page['erreur'] = array();
$page['adsm'] = array();

# form values
$page['value_id'] = "";
$page['value_name'] = "";
$page['value_date'] = "";
$page['value_url'] = "";
$page['value_file'] = "";
$page['value_pub_date'] = "";
$page['value_end_date'] = "";
$page['value_width'] = "";
$page['value_height'] = "";
$page['value_description'] = "";
$page['value_status'] = "";


if ($right_user['add_adsm'] or $right_user['edit_adsm']) {
    $page['show_form'] = 1;
} else {
    $page['show_form'] = '';
    $page['L_message'] = $lang['general']['acces_reserve_admin'];
}

# si l'identifiant du adsm est passe dans l'url (modification), on le recupere
if (isset($_GET['v2']) and $_GET['v2'] != "") {
    $page['value_id'] = $_GET['v2'];
} elseif (isset($_POST['id']) and $_POST['id'] != "") {
    $page['value_id'] = $_POST['id'];
}

# case of add or edit
if (isset($_POST) and !empty($_POST)) {
    # we format datas
    if (isset($_POST['name']))
        $_POST['name'] = format_txt($_POST['name']);

    # we check datas
    if (!isset($_POST['name']) or $_POST['name'] == "") {
        $page['erreur'][$nb_erreur]['message'] = $lang['adsm']['E_empty_name'];
        $nb_erreur++;
    }

    if (!isset($_POST['url']) or $_POST['url'] == "") {
        $page['erreur'][$nb_erreur]['message'] = $lang['adsm']['E_empty_url'];
        $nb_erreur++;
    }
    
    if (!isset($_POST['file']) or $_POST['file'] == "") {
        $page['erreur'][$nb_erreur]['message'] = "Veuillez choisir la bannière";
        $nb_erreur++;
    }

    # there is no error in submited datas
    if ($nb_erreur == 0) {
        # case : new item to add
        if (!isset($_POST['id']) or empty($_POST['id']) and $right_user['add_adsm']) {

            $sql_add = sql_replace($sql['adsm']['insert_ads'], $_POST);
            $sgbd = sql_connect();
            $execution = sql_query($sql_add);
            if ($execution) {
                $page['L_message'] = $lang['adsm']['form_adsm_add_1'];
            } else {
                $page['L_message'] = $lang['adsm']['form_adsm_add_0'];
            }
            $page['value_id'] = sql_insert_id($sgbd);
            sql_close($sgbd);
        }
        # case : item to modify
        elseif ($right_user['edit_adsm']) {
            $sql_modification = sql_replace($sql['adsm']['edit_ads'], $_POST);
            $sgbd = sql_connect();
            if (sql_query($sql_modification) != false) {
                $page['L_message'] = $lang['adsm']['form_adsm_edit_1'];
            } else {
                $page['L_message'] = $lang['adsm']['form_adsm_edit_0'];
            }
            sql_close($sgbd);
        }
    } else {
        # there is some errors: we show the datas again
        if (isset($_POST['id']))
            $page['value_id'] = $_POST['id'];
        if (isset($_POST['name']))
            $page['value_name'] = $_POST['name'];
        if (isset($_POST['url']))
            $page['value_url'] = $_POST['url'];
        if (isset($_POST['pub_date']))
            $page['value_pub_date'] = $_POST['pub_date'];
        if (isset($_POST['end_date']))
            $page['value_end_date'] = $_POST['end_date'];
        if (isset($_POST['width']))
            $page['value_width'] = $_POST['width'];
        if (isset($_POST['height']))
            $page['value_height'] = $_POST['height'];
        if (isset($_POST['description']))
            $page['value_description'] = $_POST['description'];
    }
}

# if the ID is known, we get the datas to show them in the form
if (isset($page['value_id']) and $page['value_id'] != "" and $nb_erreur == 0) {
    # we get the item information
    $var['id'] = $page['value_id'];
    $sql_details = sql_replace($sql['adsm']['get_ads_details'], $var);

    $sgbd = sql_connect();
    $res = sql_query($sql_details);
    $ligne = sql_fetch_array($res);
    sql_free_result($res);
    sql_close($sgbd);

    $page['value_id'] = $ligne['ads_id'];
    $page['value_name'] = $ligne['ads_name'];
    $page['value_url'] = $ligne['ads_url'];
    $page['value_pub_date'] = $ligne['ads_publish_date'];
    $page['value_end_date'] = $ligne['ads_end_date'];
    $page['value_width'] = $ligne['ads_width'];
    $page['value_height'] = $ligne['ads_height'];
    $page['value_description'] = $ligne['ads_description'];
    $page['value_type_id'] = $ligne['ads_type_id'];
    $page['value_position_id'] = $ligne['ads_position_id'];
    $page['value_file']=$ligne['ads_file'];

}

# adsms
if ($right_user['delete_adsm'] and !empty($page['value_id'])) {
    $page['adsm_delete'] = convert_url("index.php?r=" . $plugin_idurl .
        "&v1=adsm_list&v2=delete&v3=" . $page['value_id']);

} else {
    $page['adsm_delete'] = "";
}
$page['adsm_list'] = convert_url("index.php?r=" . $plugin_idurl .
    "&v1=adsm_list");

# elements du formulaire
# liste des position
$sql_liste = $sql['adsm']['get_positions'];
$sgbd = sql_connect();
$res_liste = sql_query($sql_liste);
$i = "0";
while ($ligne = sql_fetch_array($res_liste)) {
    $page['position'][$i]['id'] = $ligne['ads_position_id'];
    $page['position'][$i]['name'] = $ligne['ads_position_name'];

    if (isset($page['value_position_id']) and $page['value_position_id'] == $ligne['ads_position_id']) {
        $page['position'][$i]['selected'] = "selected";
    } else {
        $page['position'][$i]['selected'] = "";
    }
    $i++;
}
sql_free_result($res_liste);

# liste des type
$sql_liste = $sql['adsm']['get_types'];
$res_liste = sql_query($sql_liste);
$i = "0";
while ($ligne = sql_fetch_array($res_liste)) {
    $page['type'][$i]['value_type_id'] = $ligne['ads_type_id'];
    $page['type'][$i]['value_type_name'] = $ligne['ads_type_name'];
    
    if (isset($page['value_type_id']) and $page['value_type_id'] == $ligne['ads_type_id']) {
        $page['type'][$i]['selected'] = "selected";
    } else {
        $page['type'][$i]['selected'] = "";
    }
    $i++;
}
sql_free_result($res_liste);
sql_close($sgbd);


$page['link_choose_image']=convert_url("index.php?r=".$lang['general']['idurl_file']."&v1=image_manager&parent_form=form_adsm&field_name=file&file_type=image&fen=pop",0);

# text
if (empty($page['value_id'])) {
    $page['L_title'] = $lang['adsm']['form_adsm_add'];
} else {
    $page['L_title'] = $lang['adsm']['form_adsm_edit'];
}
$page['L_valider'] = $lang['adsm']['submit'];
$page['L_delete'] = $lang['adsm']['delete'];
$page['L_back_list'] = $lang['adsm']['back_list'];
$page['L_choose_position'] = $lang['adsm']['choose_position'];
$page['L_choose_type'] = $lang['adsm']['choose_type'];
$page['L_url'] = $lang['adsm']['URL'];
$page['L_pub_date'] = $lang['adsm']['publish_date'];
$page['L_end_date'] = $lang['adsm']['end_date'];
$page['L_width'] = $lang['adsm']['width'];
$page['L_height'] = $lang['adsm']['height'];
$page['L_choose_statut'] = $lang['adsm']['choose_statut'];
$page['L_file'] = $lang['adsm']['file'];

$page['L_erreur'] = $lang['general']['E_erreur'];
$page['L_field_required'] = $lang['general']['field_required'];

$page['L_name'] = $lang['adsm']['name'];
//$page['L_position'] = $lang['adsm']['position'];
$page['L_description'] = $lang['adsm']['description'];
//$page['L_status'] = $lang['adsm']['status'];

$page['meta_title'] = $page['L_title'];
$page['template'] = $tpl['adsm']['form_adsm'];
?>